The fundamental truth is that a website isn't just a brochure for you to read. For the web to be useful, it needs a way to collect information from the user and send it back to the server.
Without this, you couldn't log in, search for a video, buy a product, post a comment, or send a message. The web would be a read-only library.
How do we create a standardized, reliable system to:
The logical solution to this entire problem is the HTML <form>.
<input>
Element: The Box for Your Stuff
This is the most common form tag. It's a self-closing tag that creates an
input field. Its behavior changes based on its
type
attribute.
The most basic type is text:
<input type="text">
<form>
<input type="text">
</form>
<label>
The Problem: We have a box, but the user has no idea what they are supposed to type into it. Is it for a name? An email? A search query? The box is meaningless without a description.
The Solution: We need to add a descriptive piece of text.
The correct HTML tag for this is the <label>
.
<label>First Name:</label>
<input type="text">
id
and for
We connect the label and input using id
and for
:
<label for="firstName">First Name:</label>
<input type="text" id="firstName">
<form>
and submit<form>
<label for="firstName">First Name:</label>
<input type="text" id="firstName">
<br><br>
<label for="lastName">Last Name:</label>
<input type="text" id="lastName">
<br><br>
<input type="submit">
</form>
name
Attribute<form>
<label for="firstName">First Name:</label>
<input type="text" id="firstName" name="firstName">
<br><br>
<label for="lastName">Last Name:</label>
<input type="text" id="lastName" name="lastName">
<br><br>
<input type="submit">
</form>
<label>T-Shirt Size:</label><br>
<input type="radio" id="sizeS" name="shirtSize" value="small">
<label for="sizeS">Small</label><br>
<input type="radio" id="sizeM" name="shirtSize" value="medium">
<label for="sizeM">Medium</label><br>
<input type="radio" id="sizeL" name="shirtSize" value="large">
<label for="sizeL">Large</label><br><br>
<label>Pizza Toppings:</label><br>
<input type="checkbox" id="toppingPep" name="toppings" value="pepperoni">
<label for="toppingPep">Pepperoni</label><br>
<input type="checkbox" id="toppingMush" name="toppings" value="mushrooms">
<label for="toppingMush">Mushrooms</label><br>
<input type="checkbox" id="toppingOni" name="toppings" value="onions">
<label for="toppingOni">Onions</label><br><br>
<button>
Element<button type="submit">
<strong>Submit</strong> Your Order
</button>
<textarea>
<label for="comments">Any special instructions?</label><br>
<textarea id="comments" name="comments" rows="4" cols="50"></textarea><br><br>
<select>
)
<label for="country">Country:</label><br>
<select id="country" name="country">
<option value="">--Please choose an option--</option>
<option value="in">India</option>
<option value="us">USA</option>
<option value="uk">United Kingdom</option>
<option value="au">Australia</option>
</select><br><br>
<label for="userPass">Password:</label><br>
<input type="password" id="userPass" name="userPassword"><br><br>
<label for="userAge">Age (18-99):</label><br>
<input type="number" id="userAge" name="age" min="18" max="99"><br><br>
<label for="birthDate">Date of Birth:</label><br>
<input type="date" id="birthDate" name="dob"><br><br>